package util.page1;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//分页信息
public class PageUtils<T> {


    public PageUtils() {
        super();
    }

    public PageUtils(Integer pageIndex, Integer pageSize) {
        super();
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    private Integer pageIndex = 1;

    private Integer pageSize = 10;

    private Integer pageCount = 0;

    private List<T> dataList;

    private Map<String, Object> dataMap;

    public Map<String, Object> getDataMap() {
        return dataMap;
    }

    public void setDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
    }

    private Integer totalCount = 0;

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> list) {
        this.dataList = list;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }


    public Integer getPageCount() {
        pageCount = (int) Math.ceil(this.totalCount * 1.0 / this.pageSize);
        return pageCount;
    }

    protected List<T> items = new ArrayList<T>();

    /**
     * 根据pageSize与totalCount计算总页数, 默认值为0.
     */
    public long getTotalPages() {
        if (totalCount < 0) {
            return 0;
        }

        long count = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return (pageIndex + 1 <= getTotalPages());
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从0开始.
     */
    public int getFirst() {
        return (pageIndex - 1) * pageSize;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public int getShowSize(){
        if(null == items){
            return getFirst();
        }

        return getFirst() + items.size();
    }

}
